package com.example.tenant.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tenant.entity.SysUser;
import com.example.tenant.mapper.SysUserMapper;
import com.example.tenant.service.*;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 行政区划
 *
 * @author waterx
 * @date 2024-02-16 22:40:06
 */
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    private final SysUserPostService sysUserPostService;

    private final SysUserRoleService sysUserRoleService;

    private final SysUserDeptService sysUserDeptService;

    private final SysRGroupUserService sysRGroupUserService;

    @SneakyThrows
    @Override
    public void export(Long tenantId, FileOutputStream outputStream, String filePath) {

        List<SysUser> userList = this.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getTenantId, tenantId));
        List<String> userIds = userList.stream().map(SysUser::getUserId).collect(Collectors.toList());

        sysUserPostService.export(userIds, filePath);

        sysUserRoleService.export(userIds, filePath);

        sysUserDeptService.export(userIds, filePath);

        sysRGroupUserService.export(userIds, filePath);

        try (ExcelWriter excelWriter = EasyExcel.write(outputStream, entityClass).build()) {
            // 创建工作表
            WriteSheet writeSheet = EasyExcel.writerSheet("sheet").build();
            // 写入数据
            excelWriter.write(userList, writeSheet);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
